﻿// Decompiled by AS3 Sorcerer 6.12
// www.as3sorcerer.com

//Level

package 
{
    import flash.display.Sprite;
    import flash.geom.Point;
    import flash.utils.setInterval;
    import flash.utils.clearInterval;
    import flash.utils.*;

    public class Level extends Sprite 
    {

        public static const PHASE_IN_PROGRESS:int = 0;
        public static const PHASE_LEVEL_PASSED:int = 1;
        public static const PHASE_LEVEL_FAILED:int = 2;
        public static const AI_BASIC:int = 0;
        public static const AI_ADVANCED:int = 1;
        private static const MAX_CELLS:int = 30;
        private static const MAX_FENCES:int = 50;
        private static const BmpBack:Class = Level_BmpBack;

        private var m_game:Main = null;
        private var _SafeStr_91:Boolean = false;
        private var _SafeStr_84:int = 0;
        private var _SafeStr_136:uint = 0;
        private var _SafeStr_138:uint = 0;
        private var _SafeStr_139:Boolean = false;
        private var _SafeStr_97:Boolean = false;
        private var _SafeStr_131:int = Cell.TYPE_GREEN;
        private var _SafeStr_127:Sprite = null;
        private var _SafeStr_85:Sprite = null;
        private var _SafeStr_130:Array = null;
        private var _SafeStr_143:int = Cell.TYPE_BLACK;
        private var _SafeStr_135:Array = null;
        private var _SafeStr_141:uint = 0;
        private var _SafeStr_137:String = null;
        private var _SafeStr_128:Array = null;

        public function Level(game:Main, name:String="", maxPower:int=200)
        {
            this.m_game = game;
            this._SafeStr_137 = name;
            this._SafeStr_84 = maxPower;
            this._SafeStr_128 = new Array();
            this._SafeStr_130 = new Array();
            this._SafeStr_135 = new Array();
            this._SafeStr_127 = new Sprite();
            this._SafeStr_85 = new Sprite();
            addChild(this._SafeStr_127);
            addChild(this._SafeStr_85);
            this._SafeStr_136 = 100;
            this._SafeStr_144(true);
        }

        public function get maxPower():int
        {
            return (this._SafeStr_84);
        }

        public function set maxPower(power:int):void
        {
            var cell:Cell;
            this._SafeStr_84 = power;
            var i:int;
            while (i < this._SafeStr_128.length)
            {
                cell = this._SafeStr_128[i];
                cell.maxPower = power;
                i++;
            };
        }

        private function _SafeStr_129():void
        {
            var link:Link;
            var orig:Cell;
            var dest:Cell;
            var cut:Boolean;
            var i:int;
            while (i < this._SafeStr_130.length)
            {
                link = this._SafeStr_130[i];
                orig = link.getOrigCell();
                dest = link.getDestCell();
                if (orig.type != this._SafeStr_131)
                {
                    if ((orig.power < Cell.MIN_PANIC_POWER))
                    {
                        link.cutAt(1);
                    };
                    if (((orig.type == dest.type) && (link.isAttached())))
                    {
                        cut = false;
                        if (orig.type == Cell.TYPE_PURPLE)
                        {
                            cut = ((dest.power / 2) > orig.power);
                        }
                        else
                        {
                            cut = (dest.power > 10);
                        };
                        if (cut)
                        {
                            if ((dest.power + link.power) <= orig.power)
                            {
                                link.cutAt(0);
                            }
                            else
                            {
                                link.cutAt(0.5);
                            };
                        };
                    };
                    if (((orig.type == Cell.TYPE_PURPLE) && (link.isAttached())))
                    {
                        if (((dest.getGiveUpPower() < link.power) && (this._SafeStr_132(dest, orig) == null)))
                        {
                            link.cutAt(0);
                        };
                    };
                };
                i++;
            };
        }

        private function _SafeStr_132(orig:Cell, dest:Cell):Link
        {
            var link:Link;
            var i:int;
            while (i < this._SafeStr_130.length)
            {
                link = this._SafeStr_130[i];
                if ((((link.getOrigCell() == orig) && (link.getDestCell() == dest)) && (!(link.isCut()))))
                {
                    return (link);
                };
                i++;
            };
            return (null);
        }

        private function _SafeStr_133(cellType:int):void
        {
            var tmpOrig:Cell;
            var tmpDest:Cell;
            var dx:Number;
            var dy:Number;
            var len:Number;
            var i:int;
            var j:int;
            var orig:Cell;
            var dest:Cell;
            var minLen:Number = 10000;
            i = 0;
            while (i < this._SafeStr_128.length)
            {
                tmpOrig = this._SafeStr_128[i];
                if (((tmpOrig.type == cellType) && (tmpOrig.numOrigLinks < tmpOrig.maxOrigLinks)))
                {
                    j = 0;
                    while (j < this._SafeStr_128.length)
                    {
                        tmpDest = this._SafeStr_128[j];
                        if (((((tmpDest.type == tmpOrig.type) && (this._SafeStr_132(tmpDest, tmpOrig) == null)) && (this._SafeStr_132(tmpOrig, tmpDest) == null)) && (!(this.isFenceBetween(tmpOrig, tmpDest)))))
                        {
                            dx = (tmpDest.x - tmpOrig.x);
                            dy = (tmpDest.y - tmpOrig.y);
                            len = Math.sqrt(((dx * dx) + (dy * dy)));
                            if (((len < minLen) && (((tmpOrig.power - Link.getRequiredPower(len)) - 10) > tmpDest.power)))
                            {
                                minLen = len;
                                orig = tmpOrig;
                                dest = tmpDest;
                            };
                        };
                        j++;
                    };
                };
                i++;
            };
            if (((orig) && (dest)))
            {
                this.addLinkBetween(orig, dest);
            };
        }

        public function delCell(cell:Cell):void
        {
            var i:int = this._SafeStr_128.indexOf(cell);
            if (i >= 0)
            {
                this._SafeStr_128.splice(i, 1);
                this._SafeStr_85.removeChild(cell);
            };
        }

        private function _SafeStr_134(p11x:Number, p11y:Number, p12x:Number, p12y:Number, p21x:Number, p21y:Number, p22x:Number, p22y:Number):Point
        {
            var found:Boolean;
            var resX:Number = 0;
            var resY:Number = 0;
            var z:Number = (((p12y - p11y) * (p21x - p22x)) - ((p21y - p22y) * (p12x - p11x)));
            var ca:Number = (((p12y - p11y) * (p21x - p11x)) - ((p21y - p11y) * (p12x - p11x)));
            var cb:Number = (((p21y - p11y) * (p21x - p22x)) - ((p21y - p22y) * (p21x - p11x)));
            if (z == 0)
            {
                return (null);
            };
            var ua:Number = (ca / z);
            var ub:Number = (cb / z);
            resX = (p11x + ((p12x - p11x) * ub));
            resY = (p11y + ((p12y - p11y) * ub));
            if (((((0 <= ua) && (ua <= 1)) && (0 <= ub)) && (ub <= 1)))
            {
                if (!((((ua == 0) || (ua == 1)) || (ub == 0)) || (ub == 1)))
                {
                    return (new Point(resX, resY));
                };
            };
            return (null);
        }

        public function addFence(x1:Number, y1:Number, x2:Number, y2:Number):Fence
        {
            if (this._SafeStr_135.length >= MAX_FENCES)
            {
                return (null);
            };
            var fence:Fence = new Fence(x1, y1, x2, y2);
            this._SafeStr_85.addChild(fence);
            this._SafeStr_135.push(fence);
            return (fence);
        }

        public function newAchievement(id:int):void
        {
            this.m_game.newAchievement(id);
        }

        public function destruct():void
        {
            this._SafeStr_144(false);
            if (parent)
            {
                parent.removeChild(this);
            };
            this.clear();
        }

        public function getUserCellType():int
        {
            return (this._SafeStr_131);
        }

        public function addCell(x:Number, y:Number, _arg_3:int, power:int, maxPower:int=999):Cell
        {
            if (this._SafeStr_128.length >= MAX_CELLS)
            {
                return (null);
            };
            var cell:Cell = new Cell(this, _arg_3, power, maxPower);
            cell.x = x;
            cell.y = y;
            if (this._SafeStr_91)
            {
                cell.suspend();
            };
            this._SafeStr_85.addChild(cell);
            this._SafeStr_128.push(cell);
            if (_arg_3 == Cell.TYPE_BLACK)
            {
                this._SafeStr_139 = true;
            };
            return (cell);
        }

        public function delLink(link:Link):void
        {
            var i:int = this._SafeStr_130.indexOf(link);
            if (i >= 0)
            {
                this._SafeStr_130.splice(i, 1);
                this._SafeStr_127.removeChild(link);
            };
        }

        public function get levelName():String
        {
            return (this._SafeStr_137);
        }

        public function suspend():void
        {
            var cell:Cell;
            this._SafeStr_91 = true;
            var i:int;
            while (i < this._SafeStr_128.length)
            {
                cell = this._SafeStr_128[i];
                cell.suspend();
                i++;
            };
        }

        public function onLinkAttached(link:Link):void
        {
            this.m_game.onLinkAttached(link);
        }

        private function _SafeStr_140(cellType:int):void
        {
            var tmpOrig:Cell;
            var tmpDest:Cell;
            var dx:Number;
            var dy:Number;
            var len:Number;
            var i:int;
            var j:int;
            var orig:Cell;
            var dest:Cell;
            var minLen:Number = 10000;
            i = 0;
            while (i < this._SafeStr_128.length)
            {
                tmpOrig = this._SafeStr_128[i];
                if (((tmpOrig.type == cellType) && (tmpOrig.numOrigLinks < tmpOrig.maxOrigLinks)))
                {
                    j = 0;
                    while (j < this._SafeStr_128.length)
                    {
                        tmpDest = this._SafeStr_128[j];
                        if ((((!(tmpDest.type == tmpOrig.type)) && (this._SafeStr_132(tmpOrig, tmpDest) == null)) && (!(this.isFenceBetween(tmpOrig, tmpDest)))))
                        {
                            dx = (tmpDest.x - tmpOrig.x);
                            dy = (tmpDest.y - tmpOrig.y);
                            len = Math.sqrt(((dx * dx) + (dy * dy)));
                            if (this._SafeStr_132(tmpDest, tmpOrig) != null)
                            {
                                len = (len / 2);
                            };
                            if (((len < minLen) && ((tmpOrig.power - 10) > Link.getRequiredPower(len))))
                            {
                                minLen = len;
                                orig = tmpOrig;
                                dest = tmpDest;
                            };
                        };
                        j++;
                    };
                };
                i++;
            };
            if (((orig) && (dest)))
            {
                this.addLinkBetween(orig, dest);
            };
        }

        public function set cpuTurnPeriod(period:uint):void
        {
            this._SafeStr_136 = period;
        }

        public function addLinkBetween(orig:Cell, dest:Cell):Boolean
        {
            if (this._SafeStr_132(orig, dest) != null)
            {
                return (false);
            };
            if (this.isFenceBetween(orig, dest))
            {
                return (false);
            };
            var inverseLink:Link = this._SafeStr_132(dest, orig);
            var link:Link = new Link(this, orig, dest);
            this._SafeStr_130.push(link);
            this._SafeStr_127.addChild(link);
            if (inverseLink)
            {
                if (orig.type == dest.type)
                {
                    inverseLink.cutAt(1);
                };
                if (!inverseLink.isCut())
                {
                    inverseLink.setSplitLink(link);
                };
            };
            return (true);
        }

        private function clear():void
        {
            var cell:Cell;
            var fence:Fence;
            while (this._SafeStr_128.length > 0)
            {
                cell = this._SafeStr_128[0];
                cell.destruct();
                cell = null;
            };
            while (this._SafeStr_135.length > 0)
            {
                fence = this._SafeStr_135[0];
                this.delFence(fence);
                fence.destruct();
                fence = null;
            };
        }

        public function cutLink(x1:Number, y1:Number, x2:Number, y2:Number):void
        {
            var link:Link;
            var p21x:Number;
            var p21y:Number;
            var p22x:Number;
            var p22y:Number;
            var res:Point;
            var foundLink:Link;
            var resX:Number = 0;
            var resY:Number = 0;
            var i:int;
            while (i < this._SafeStr_130.length)
            {
                link = this._SafeStr_130[i];
                p21x = link.getOrigAnchor().x;
                p21y = link.getOrigAnchor().y;
                p22x = link.getDestAnchor().x;
                p22y = link.getDestAnchor().y;
                res = this._SafeStr_134(x1, y1, x2, y2, p21x, p21y, p22x, p22y);
                if (((!(res == null)) && (link.getOrigCell().type == this._SafeStr_131)))
                {
                    foundLink = link;
                    foundLink.cutAtPoint(res.x, res.y);
                };
                i++;
            };
            if (foundLink)
            {
                this.m_game.clearTutorial();
                this.m_game.newAchievement(Achievements.ID_CUT);
            };
        }

        public function getTotalPowerOf(cellType:int):int
        {
            var cell:Cell;
            var total:int;
            var i:int;
            while (i < this._SafeStr_128.length)
            {
                cell = this._SafeStr_128[i];
                if (cell.type == cellType)
                {
                    total = (total + cell.power);
                };
                i++;
            };
            return (total);
        }

        public function isFenceBetween(orig:Cell, dest:Cell):Boolean
        {
            var fence:Fence;
            var dx:Number;
            var dy:Number;
            var ang:Number;
            var x1:Number;
            var y1:Number;
            var x2:Number;
            var y2:Number;
            var EXT_LEN:int = 10;
            var i:int;
            while (i < this._SafeStr_135.length)
            {
                fence = this._SafeStr_135[i];
                dx = (fence.x2 - fence.x1);
                dy = (fence.y2 - fence.y1);
                ang = Math.atan2(dy, dx);
                x1 = (fence.x1 - (Math.cos(ang) * EXT_LEN));
                y1 = (fence.y1 - (Math.sin(ang) * EXT_LEN));
                x2 = (fence.x2 + (Math.cos(ang) * EXT_LEN));
                y2 = (fence.y2 + (Math.sin(ang) * EXT_LEN));
                if (this._SafeStr_134(orig.x, orig.y, dest.x, dest.y, x1, y1, x2, y2) != null)
                {
                    return (true);
                };
                i++;
            };
            return (false);
        }

        public function getCode():String
        {
            var cell:Cell;
            var fence:Fence;
            var code:String = "";
            var checkSum:int = ((((this._SafeStr_128.length + this._SafeStr_135.length) + this._SafeStr_84) + this._SafeStr_141) + this._SafeStr_136);
            var isUserCell:Boolean;
            var isEnemyCell:Boolean;
            code = String((((((((((this._SafeStr_128.length + ",") + this._SafeStr_135.length) + ",") + this._SafeStr_84) + ",") + this._SafeStr_141) + ",") + this._SafeStr_136) + ","));
            var i:int;
            i = 0;
            while (i < this._SafeStr_128.length)
            {
                cell = this._SafeStr_128[i];
                code = (code + String((((((((cell.type + ",") + cell.power) + ",") + int(cell.x)) + ",") + int(cell.y)) + ",")));
                checkSum = (checkSum + (((cell.type + cell.power) + int(cell.x)) + int(cell.y)));
                if (cell.type == Cell.TYPE_GREEN)
                {
                    isUserCell = true;
                }
                else
                {
                    if (cell.type != Cell.TYPE_GREY)
                    {
                        isEnemyCell = true;
                    };
                };
                i++;
            };
            i = 0;
            while (i < this._SafeStr_135.length)
            {
                fence = this._SafeStr_135[i];
                code = (code + String((((((((int(fence.x1) + ",") + int(fence.y1)) + ",") + int(fence.x2)) + ",") + int(fence.y2)) + ",")));
                checkSum = (checkSum + (((int(fence.x1) + int(fence.y1)) + int(fence.x2)) + int(fence.y2)));
                i++;
            };
            code = (code + String(checkSum));
            return (((isUserCell) && (isEnemyCell)) ? code : null);
        }

        private function _SafeStr_142():void
        {
            var j:int;
            var dx:Number;
            var dy:Number;
            var len:Number;
            var ang:Number;
            var value:Number;
            var orig:Cell;
            var dest:Cell;
            var borderDelta:Number = 5;
            var i:int;
            while (i < this._SafeStr_128.length)
            {
                orig = this._SafeStr_128[i];
                if ((orig.x - orig.radius()) < 0)
                {
                    orig.addMoveDelta(0, borderDelta);
                };
                if ((orig.x + orig.radius()) > GUI.MAX_X)
                {
                    orig.addMoveDelta(180, borderDelta);
                };
                if ((orig.y - orig.radius()) < 20)
                {
                    orig.addMoveDelta(90, borderDelta);
                };
                if ((orig.y + orig.radius()) > (GUI.MAX_Y - 30))
                {
                    orig.addMoveDelta(-90, borderDelta);
                };
                j = 0;
                j = (i + 1);
                while (j < this._SafeStr_128.length)
                {
                    dest = this._SafeStr_128[j];
                    dx = (dest.x - orig.x);
                    dy = (dest.y - orig.y);
                    len = Math.sqrt(((dx * dx) + (dy * dy)));
                    if (((orig.radius() + dest.radius()) + 40) > len)
                    {
                        ang = dest.getAngleTo(orig);
                        value = (((orig.radius() + dest.radius()) > len) ? 20 : (200 / len));
                        orig.addMoveDelta(ang, value);
                        dest.addMoveDelta((ang + 180), value);
                    };
                    j++;
                };
                i++;
            };
        }

        public function delFence(fence:Fence):void
        {
            var i:int = this._SafeStr_135.indexOf(fence);
            if (i >= 0)
            {
                this._SafeStr_135.splice(i, 1);
                this._SafeStr_85.removeChild(fence);
            };
        }

        public function get cpuTurnPeriod():uint
        {
            return (this._SafeStr_136);
        }

        public function initFromCode(code:String):Boolean
        {
            var _local_7:int;
            var power:int;
            var x:int;
            var y:int;
            var x1:int;
            var y1:int;
            var x2:int;
            var y2:int;
            var sub:Array = code.split(",");
            var numCells:int = int((sub.shift() as String));
            var numFences:int = int((sub.shift() as String));
            this._SafeStr_84 = int((sub.shift() as String));
            this._SafeStr_141 = int((sub.shift() as String));
            this._SafeStr_136 = int((sub.shift() as String));
            var expectedSum:int = ((((numCells + numFences) + this._SafeStr_84) + this._SafeStr_141) + this._SafeStr_136);
            while (((sub.length > 0) && (numCells > 0)))
            {
                _local_7 = int((sub.shift() as String));
                power = int((sub.shift() as String));
                x = int((sub.shift() as String));
                y = int((sub.shift() as String));
                this.addCell(x, y, _local_7, power, this._SafeStr_84);
                expectedSum = (expectedSum + (((_local_7 + power) + x) + y));
                numCells--;
            };
            while (((sub.length > 0) && (numFences > 0)))
            {
                x1 = int((sub.shift() as String));
                y1 = int((sub.shift() as String));
                x2 = int((sub.shift() as String));
                y2 = int((sub.shift() as String));
                this.addFence(x1, y1, x2, y2);
                expectedSum = (expectedSum + (((x1 + y1) + x2) + y2));
                numFences--;
            };
            var checkSum:int = int((sub.shift() as String));
            if (checkSum != expectedSum)
            {
                this.clear();
                return (false);
            };
            return (true);
        }

        private function _SafeStr_144(enabled:Boolean):void
        {
            if (enabled)
            {
                if (this._SafeStr_138 == 0)
                {
                    this._SafeStr_138 = setInterval(this._SafeStr_142, 1000);
                };
            }
            else
            {
                if (this._SafeStr_138 != 0)
                {
                    clearInterval(this._SafeStr_138);
                    this._SafeStr_138 = 0;
                };
            };
        }

        public function onCpuTurn():void
        {
            this._SafeStr_140(Cell.TYPE_RED);
            this._SafeStr_140(Cell.TYPE_BLACK);
            this._SafeStr_140(Cell.TYPE_PURPLE);
            this._SafeStr_133(Cell.TYPE_PURPLE);
            this._SafeStr_129();
        }

        public function getGamePhase():int
        {
            var cell:Cell;
            var _local_1:int = -1;
            var someUserCells:Boolean;
            var inProgressYet:Boolean;
            var i:int;
            while (i < this._SafeStr_128.length)
            {
                cell = this._SafeStr_128[i];
                if (cell.type == this._SafeStr_131)
                {
                    someUserCells = true;
                };
                if (((!(cell.type == _local_1)) && (!(cell.type == Cell.TYPE_GREY))))
                {
                    if (_local_1 == -1)
                    {
                        _local_1 = cell.type;
                    }
                    else
                    {
                        inProgressYet = true;
                    };
                };
                i++;
            };
            if (!someUserCells)
            {
                return (PHASE_LEVEL_FAILED);
            };
            return ((inProgressYet) ? PHASE_IN_PROGRESS : PHASE_LEVEL_PASSED);
        }

        public function pause(enabled:Boolean):void
        {
            var cell:Cell;
            var link:Link;
            var i:int;
            if (this._SafeStr_97 == enabled)
            {
                return;
            };
            this._SafeStr_97 = enabled;
            this._SafeStr_144(enabled);
            i = 0;
            while (i < this._SafeStr_128.length)
            {
                cell = this._SafeStr_128[i];
                cell.pause(enabled);
                i++;
            };
            i = 0;
            while (i < this._SafeStr_130.length)
            {
                link = this._SafeStr_130[i];
                link.pause(enabled);
                i++;
            };
        }


    }
}//package 

// _SafeStr_127 = "do" (String#6089, DoABC#2)
// _SafeStr_128 = "set " (String#7366, DoABC#2)
// _SafeStr_129 = "_-2N" (String#4769, DoABC#2)
// _SafeStr_130 = "_-10" (String#4700, DoABC#2)
// _SafeStr_131 = "_-AS" (String#5155, DoABC#2)
// _SafeStr_132 = "else" (String#6132, DoABC#2)
// _SafeStr_133 = "_-BX" (String#5205, DoABC#2)
// _SafeStr_134 = "use " (String#7678, DoABC#2)
// _SafeStr_135 = "_-9x" (String#5125, DoABC#2)
// _SafeStr_136 = "_-9y" (String#5126, DoABC#2)
// _SafeStr_137 = "_-5x" (String#4931, DoABC#2)
// _SafeStr_138 = "_-8z" (String#5080, DoABC#2)
// _SafeStr_139 = "_-CJ" (String#5240, DoABC#2)
// _SafeStr_140 = "_-AU" (String#5157, DoABC#2)
// _SafeStr_141 = "_-2k" (String#4785, DoABC#2)
// _SafeStr_142 = "_-7F" (String#4998, DoABC#2)
// _SafeStr_143 = "_-AP" (String#5152, DoABC#2)
// _SafeStr_144 = "_-j" (String#5291, DoABC#2)
// _SafeStr_84 = "_-AL" (String#5149, DoABC#2)
// _SafeStr_85 = "_-CY" (String#5251, DoABC#2)
// _SafeStr_91 = "_-4M" (String#4858, DoABC#2)
// _SafeStr_97 = "_-1I" (String#4713, DoABC#2)


